home *** CD-ROM | disk | FTP | other *** search
- Path: news.ov.com!news
- From: glenn@ov.com (Fletcher.Glenn@ov.com)
- Newsgroups: comp.lang.c
- Subject: Re: char* still alive after free ???
- Date: 15 Apr 1996 20:24:04 GMT
- Organization: OpenVision
- Message-ID: <4kub94$5nm@spanky.pls.ov.com>
- References: <317269EA.11BB93C2@studbox.uni-stuttgart.de>
- Reply-To: glenn@ov.com
- NNTP-Posting-Host: foghorn.pls.ov.com
-
- In article 11BB93C2@studbox.uni-stuttgart.de, Markus Heller <Markus.Heller@studbox.uni-stuttgart.de> writes:
- >Hi,
- >
- >I have a global variable :
- >
- >char *text=NULL;
- >
- >I want to use it to store different "strings" (at diffreent times).
- >E.g., if I want to sore 10 characters in text, I do a
- >text=(char *) malloc(10*sizeof(char));
- >When I want to use text to store 3 other characters, I first do a
- >free(text); text=NULL; and finally a
- >text=(char *) malloc(3*sizeof(char));
- >But to my surprise there are still the 4thh to 10th charcter of
- >text contained before the free(text)/malloc... ???
- >This happens under linux, but why ?
- >(what I want to to is to get filenames from the user and then open those
- > files by a function to which I pass the file name..)
- >
- >Markus
-
-
- 1. There is no requirement for the memory allocated by malloc() to
- be initialized. The user is responsible for initialization.
-
- 2. malloc() may re-use memory made available by free().
-
- 3. You were violating the terms of malloc by looking past the
- third char. You could have caused some form of memory protection
- error. BTW, if you were going to store strings, don't forget
- to make room for the trailing null character in your memory allocation
- requests.
-
- Fletcher.Glenn@ov.com
-
-